package com.cuit.qx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.qx.aop.OperLog;
import com.cuit.qx.bean.OperationLog;
import com.cuit.qx.service.OperationLogService;
import com.cuit.qx.utils.ReturnData;
import com.cuit.qx.utils.ReturnStateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


import static org.thymeleaf.util.ListUtils.isEmpty;

/**
 * @Author:wjd
 * @Description: 日志操作Controller层
 * @date:Created in 10:35 2020/9/15
 * @Modified By:
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/api/sysLog")
public class OperationLogController {

    @Autowired
    OperationLogService operationLogService;

    /**
     * @return
     * @Author:wjd on 2020/9/16 14:48
     * @param: pageNo页数   pageSize 每页的条数
     * @Description: 查询所有日志
     */
    @RequestMapping("/getAllLog")
    @ResponseBody
    @OperLog(operModul = "日志管理", operType = "1", operDesc = "查询日志信息")
    public ReturnData getAllLog(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        List<OperationLog> allLogData = operationLogService.getAllLogData(pageNo, pageSize);
        if (!isEmpty(allLogData)) {
            return new ReturnData(ReturnStateCode.SUCCEE.getCode(), ReturnStateCode.SUCCEE.getMsg(), allLogData);
        } else {
            return new ReturnData(ReturnStateCode.SUCCEEBUT.getCode(), ReturnStateCode.SUCCEEBUT.getMsg());
        }

    }

    /**
     * 根据条件查询日志
     *
     * @param pageNo    传入的页数
     * @param pageSize  每页条数
     * @param operModul 操作类型
     * @param handlers  操作人
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping(value = "/getConditionLog", method = RequestMethod.GET)
    @OperLog(operModul = "日志管理", operType = "1", operDesc = "根据条件查询日志信息")
    @ResponseBody
    public ReturnData getConditionLog(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      String operModul, String operType, String handlers, String startTime, String endTime
    ) {
        List<OperationLog> conditionLogData = operationLogService.getConditionLogData(pageNo, pageSize, operModul, operType, handlers, startTime, endTime);
        int count=operationLogService.getCount(operModul, operType, handlers, startTime, endTime);
        if (!isEmpty(conditionLogData)) {
            return new ReturnData(ReturnStateCode.SUCCEE.getCode(), ReturnStateCode.SUCCEE.getMsg(),count, conditionLogData);
        } else {
            return new ReturnData(ReturnStateCode.SUCCEEBUT.getCode(), ReturnStateCode.SUCCEEBUT.getMsg(),count,conditionLogData);
        }
    }

}
